package com.koron.css2.baseConfig.impl;

import com.github.pagehelper.PageHelper;
import com.koron.css2.baseConfig.mapper.BaseBookMapper;
import com.koron.css2.baseConfig.mapper.BusinessAbodeMapper;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.queryBean.BaseBookQueryBean;
import com.koron.css2.baseConfig.vo.BaseBookListVO;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.SelectBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.util.JsonUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BaseBookExport extends AbstractExportExcel {

	@Override
	public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		BaseBookQueryBean bean = JsonUtils.objectToPojo(req.getData(), BaseBookQueryBean.class);
		bean.setSortStr(StringUtils.isBlank(bean.getSortField()) || bean.getSortOrder() == null ?
				null : (bean.getSortField() + " " + (bean.getSortOrder() == 1 ? "ASC" : "DESC")));
		BaseBookMapper mapper = factory.getMapper(BaseBookMapper.class);
		//营业区域单独处理
		if (bean.getBusinessArea() !=null && !"".equals(bean.getBusinessArea())){
			BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
			List<String> areaIdList = this.queryChildrenArea(bean.getBusinessArea(),userInfo.getCurWaterCode(),businessAreaMapper);
			bean.setBusinessAreaList(areaIdList);
		}
		PageHelper.startPage(bean.getPage(), bean.getPageCount());
		List<BaseBookListVO> list = mapper.select(bean);

		//营业所转换
		BusinessAbodeMapper abodemapper = factory.getMapper(BusinessAbodeMapper.class,"_default");
		// 获取营业区域

		//获取下拉框
		List<SelectVO> Abodes = abodemapper.businessAbodeSelect(userInfo.getCurWaterCode());
		Map<String, String> RTP = Tools.mapDicByCode(factory, "RTP");
		for (BaseBookListVO vo : list) {
			for (SelectVO abode : Abodes) {
				if(abode.getId().equals(vo.getBusinessAbode())) {
					vo.setBusinessAbodeName(abode.getName());
				}
			}
			vo.setMeterReadingCycleName(RTP.get(vo.getMeterReadingCycle()));
			if(vo.getStatus() == 1) {
				vo.setStatusName("启用");
			}else {
				vo.setStatusName("停用");
			}
			if (!StringUtils.isEmpty(vo.getBusinessArea())) {
				BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
				vo.setBusinessAreaName(
						businessAreaMapper.findBusinessAreaById(vo.getBusinessArea(), userInfo.getCurWaterCode()));
			}
		}
		
		// 返回结果 FnInvoiceExport
		AttachmentDto attachment = new AttachmentDto();
		attachment.setBtlTemplate("BaseBookExport.btl");
		attachment.setFileTemplate("BaseBookExport.xlsx");
		attachment.setData(list);
		attachment.setFileType("excel");
		attachment.setFilename("册本信息" + CommonUtils.getCurrentDate());
		return attachment;
	}
	private List<String> queryChildrenArea(String parentId,String groupCode,BusinessAreaMapper mapper){
		List<SelectBean> list = mapper.findBusinessAreaByParentId(parentId,groupCode);
		List<String> result = new ArrayList<>();
		if (list != null && list.size()>0){
			for (SelectBean selectBean : list){
				List<String> sub = queryChildrenArea(selectBean.getId(),groupCode,mapper);
				result.addAll(sub);
			}
		}else {
			result.add(parentId);
		}
		//result.addAll(list.stream().map(SelectBean::getCode).collect(Collectors.toList()));

		return result;
	}

}
